package com.wonders.module.auth.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wonders.common.constant.CommonConstant;
import com.wonders.framework.dao.impl.BaseDaoImpl;
import com.wonders.framework.entity.vo.Page;
import com.wonders.framework.entity.vo.Parameter;
import com.wonders.module.auth.dao.RoleDao;
import com.wonders.module.auth.entity.bo.Role;
import com.wonders.module.auth.entity.vo.RoleVo;
import com.wonders.module.auth.entity.vo.UserRoleVo;

/**
 * 角色信息数据层实现
 * 
 * @author WuBin
 * 
 */
@Repository
public class RoleDaoImpl extends BaseDaoImpl implements RoleDao {

	public Page query(RoleVo roleVo, Page page) {
		Criteria criteria = super.getCurrentSession()
				.createCriteria(Role.class);
		if (StringUtils.isNotBlank(roleVo.getCode())) {
			criteria.add(Restrictions.like("code", "%" + roleVo.getCode() + "%"));
		}
		if (StringUtils.isNotBlank(roleVo.getName())) {
			criteria.add(Restrictions.like("name", "%" + roleVo.getName() + "%"));
		}
		criteria.add(Restrictions.eq("enable", roleVo.getEnable()));
		criteria.addOrder(Order.desc("id"));
		return super.pageByCriteria(criteria, page);
	}

	public void remove(List<Long> keys) {
		String hql = "update Role r set r.enable = :enable where r.id in (:keys)";
		List<Parameter> args = new ArrayList<Parameter>();
		args.add(new Parameter("enable", CommonConstant.Status.DISABLED));
		args.add(new Parameter("keys", keys));
		super.excuteHql(hql, args);
	}

	public List<Map<String, Object>> isExist(RoleVo roleVo) {
		StringBuffer sql = new StringBuffer("select key, count from (");
		List<Parameter> args = new ArrayList<Parameter>();
		if (StringUtils.isNotBlank(roleVo.getCode())) {
			sql.append("select '代码' as key, count(1) as count, '1' as seq from au_role r where r.code = :code #if");
			args.add(new Parameter("code", roleVo.getCode()));
		}
		if (StringUtils.isNotBlank(roleVo.getName())) {
			if (sql.length() > 0) {
				sql.append("union ");
			}
			sql.append("select '名称' as key, count(1) as count, '2' as seq from au_role r where r.name = :name #if");
			args.add(new Parameter("name", roleVo.getName()));
		}
		sql.append(") where count > 0 order by seq");
		if (roleVo.getId() != null) {
			args.add(new Parameter("id", roleVo.getId()));
			return super.listBySql(
					sql.toString().replaceAll("#if", "and id <> :id "), args);
		}
		return super.listBySql(sql.toString().replaceAll("#if", ""), args);
	}

	public Page queryUserRole(UserRoleVo userRoleVo, Page page) {
		String hql = null;
		if (!"notIn".equals(userRoleVo.getQueryType())) {
			hql = "select r from Role r, UserRole ur where r.id = ur.roleId and ur.userId = :userId";
		} else {
			hql = "select r from Role r where not exists (select '1' from UserRole ur where r.id = ur.roleId and ur.userId = :userId)";
		}
		List<Parameter> args = new ArrayList<Parameter>();
		args.add(new Parameter("userId", userRoleVo.getUserId()));
		return super.pageByHql(hql, args, page);
	}

}
